package com.wn.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wn.model.BarData;
import com.wn.model.Piedata;
import com.wn.model.R;
import com.wn.model.User;
import com.wn.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yellow Docter
 * @date 2022 -06-09
 * @desc
 */
@RestController
@CrossOrigin
@RequestMapping("echar")
public class EcharController {

    @Autowired
    private IUserService userService;


    @GetMapping("bar")
    public R getBarData(){

        //01 获取数据
        List<String>  names =new ArrayList<>();
        names.add("球鞋");
        names.add("笔记本电脑");
        names.add("篮球");
        names.add("拖鞋");
        names.add("扑克");

        List<Integer> nums =new ArrayList<>();

        nums.add(20);
        nums.add(10);
        nums.add(3);
        nums.add(10);
        nums.add(10);

        //02 组装数据
        BarData barData = new BarData();
        barData.setNames(names);
        barData.setNums(nums);
        //03 返回数据
        return R.OK(barData);

    }

    @GetMapping("pie")
    public R getPierData(){



        //01 获取数据
        List<Map<String,Object>> lists = new ArrayList<>();

        Map<String,Object> map1 = new HashMap<>();
        map1.put("value",1200);
        map1.put("name","吃饭");
        lists.add(map1);

        Map<String,Object> map2 = new HashMap<>();
        map2.put("value",100);
        map2.put("name","公交地铁");
        lists.add(map2);

        Map<String,Object> map3 = new HashMap<>();
        map3.put("value",12);
        map3.put("name","娱乐活动");
        lists.add(map3);

        Map<String,Object> map4 = new HashMap<>();
        map4.put("value",250);
        map4.put("name","洗脚");
        lists.add(map4);

        Map<String,Object> map5 = new HashMap<>();
        map5.put("value",500);
        map5.put("name","和女友吃饭");
        lists.add(map5);

        //02 组装数据
        Piedata data = new Piedata();
        data.setDatas(lists);
        //03 返回数据
        return R.OK(data);
    }

    @GetMapping("user")
    public R getBarUserData(){

        //01 获取数据
        List<String>  names =new ArrayList<>();
        names.add("学生");
        names.add("管理员");
        List<Integer> nums =new ArrayList<>();

        // 查询数据库

        QueryWrapper<User> qr1 = new QueryWrapper<>();
        qr1.eq("role",1);
        //统计操作
        // 查询的是学生
        Long count = userService.count(qr1);

        QueryWrapper<User> qr3 = new QueryWrapper<>();
        qr3.eq("role",3);
        //统计操作
        // 查询的是学生
        Long count3 = userService.count(qr3);


        nums.add(count.intValue());
        nums.add(count3.intValue());

        //02 组装数据
        BarData barData = new BarData();
        barData.setNames(names);
        barData.setNums(nums);
        //03 返回数据
        return R.OK(barData);

    }

}
